﻿#region Using

using System;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
using System.Globalization;
using System.Web; 

#endregion

#region Code



/// <summary>
/// TranslatePost - Makes a series of link to Translate your Post with Google Translation Services
/// </summary>
[Extension("TranslatePost - Automatic translation for your blog post by using Google Translation Services", "1.1.1", "<a href=\"http://espertini.com\">Davide Espertini</a>")]
public class TranslatePost
{
    static protected String _strPageLink = null;
    static protected String _strUserLocale = null;

	public TranslatePost()
	{
        Post.Serving += new EventHandler<ServingEventArgs>(ServingHandler);
        Page.Serving += new EventHandler<ServingEventArgs>(Page_Serving);

        CultureInfo ci = this.ResolveCulture();
        _strUserLocale = ci.TwoLetterISOLanguageName;
	}

    private void ServingHandler(object sender, ServingEventArgs e)
    {
        String strLink = null;
        String strPostLink = ((Post)(sender)).AbsoluteLink.ToString();
        String strBlogCulture = BlogEngine.Core.BlogSettings.Instance.Culture;
        
        if (e.Location == ServingLocation.SinglePost)
        {
            switch (_strUserLocale){
                case "it":
                    strLink = "<h3>Leggi l'articolo nella tua lingua ";
                    break;
                case "en":
                    strLink = "<h3>Read this article in your language ";
                    break;
                case "de":
                    strLink = "<h3>Lesen Sie diesen Artikel in Ihrer Sprache ";
                    break;
                case "es":
                    strLink = "<h3>Lee este artículo en su idioma ";
                    break;
                default:
                    strLink = "<h3>Read this article in your language ";
                    break;
            }

            strLink += "<a href='http://translate.google.com/translate?u=" + strPostLink + "%2F&sl=" + strBlogCulture + "&tl=it&hl=" + _strUserLocale + "&ie=UTF-8'>IT</a> | ";
            strLink += "<a href='http://translate.google.com/translate?u=" + strPostLink + "%2F&sl=" + strBlogCulture + "&tl=en&hl=" + _strUserLocale + "&ie=UTF-8'>EN</a> | ";
            strLink += "<a href='http://translate.google.com/translate?u=" + strPostLink + "%2F&sl=" + strBlogCulture + "&tl=de&hl=" + _strUserLocale + "&ie=UTF-8'>DE</a> | ";
            strLink += "<a href='http://translate.google.com/translate?u=" + strPostLink + "%2F&sl=" + strBlogCulture + "&tl=es&hl=" + _strUserLocale + "&ie=UTF-8'>ES</a>";
            strLink += "</h3>";

            string oldBody = e.Body;
            e.Body = strLink + oldBody;
        }

        
    }

    private void Page_Serving(object sender, ServingEventArgs e)
    {
        _strPageLink = ((Page)(sender)).AbsoluteLink.ToString();
        
    }

    #region Useful Methods

    private CultureInfo ResolveCulture()
    {
        string[] languages = HttpContext.Current.Request.UserLanguages;

        if (languages == null || languages.Length == 0)
            return null;
        try
        {
            string language = languages[0].ToLowerInvariant().Trim();
            return CultureInfo.CreateSpecificCulture(language);
        }
        catch (ArgumentException)
        {
            return null;
        }
    }

    #endregion

}

#endregion